package edu.mit.media.fv.blocks
{
	import edu.mit.media.fv.icons.*;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.utils.Timer;

	public class BlockPopup extends Sprite
	{
		private var eye:DisplayObject;
		private var thumbsup:DisplayObject;
		private var thumbsdown:DisplayObject;
		private var fadeInTimer:Timer;
		private var stopTimer:Timer;
		
		private var _innerRadius:int;
		private var _outerRadius:int;
		
		public function BlockPopup(viewFuncion:Function, thumbsupFuncton:Function, thumbsdownFunction:Function)
		{
			super();
			
			eye = new AQEyeIcon();
			this.addChild(eye);
			eye.addEventListener(MouseEvent.CLICK, viewFuncion);
			
			thumbsup = new AQThumbsUpIcon();
			this.addChild(thumbsup);
			thumbsup.addEventListener(MouseEvent.CLICK, thumbsupFuncton);
			
			thumbsdown = new AQThumbsDownIcon();
			this.addChild(thumbsdown);
			thumbsdown.addEventListener(MouseEvent.CLICK, thumbsdownFunction);
			
			this.visible = false;
			
			fadeInTimer = new Timer(40,5);
			fadeInTimer.addEventListener(TimerEvent.TIMER, onFadeTimer);
			fadeInTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onFadeComplete);
			
			stopTimer = new Timer(1000, 4);
			stopTimer.addEventListener(TimerEvent.TIMER, onStopTimer);
			stopTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onStopComplete);
			
			this.addEventListener(MouseEvent.CLICK, processMouseClicked);
		}
		public function reform(innerRadius:int, outerRadius:int = 100):void{
			this._innerRadius = innerRadius;
			this._outerRadius = outerRadius;
			
			this.graphics.clear();
			this.graphics.beginFill(0x000000, 0.8);
			this.graphics.lineStyle(1, 0xFFFFFF, 0.70, true);
			this.graphics.drawCircle(0, 0, this._outerRadius);
			this.graphics.drawCircle(0, 0, this._innerRadius);
			this.graphics.endFill();
				
			var ringCenter:int = 0.5*(this._innerRadius+this._outerRadius);
			var ringWidth:int = (this._outerRadius-this._innerRadius)/2;
			var theta:Number = Math.PI/2;
			
			this.thumbsup.x = ringCenter*Math.cos(0*theta+Math.PI)-ringWidth/2;
			this.thumbsup.y = -ringCenter*Math.sin(0*theta+Math.PI)-ringWidth/2;
			this.thumbsup.width = ringWidth;
			this.thumbsup.height = ringWidth;
			
			this.eye.x = ringCenter*Math.cos(1*theta+Math.PI)-ringWidth/2;
			this.eye.y = -ringCenter*Math.sin(1*theta+Math.PI)-ringWidth/2;
			this.eye.width = ringWidth;
			this.eye.height = ringWidth;
			
			this.thumbsdown.x = ringCenter*Math.cos(2*theta+Math.PI)-ringWidth/2;
			this.thumbsdown.y = -ringCenter*Math.sin(2*theta+Math.PI)-ringWidth/2;
			this.thumbsdown.width = ringWidth;
			this.thumbsdown.height = ringWidth;
			
		}
		private function processMouseClicked(event:MouseEvent):void{
			this.fadeInTimer.reset();
			this.stopTimer.reset();		
			this.visible = false;
		}
		private function onFadeTimer(event:TimerEvent):void{
			this.alpha = fadeInTimer.currentCount/fadeInTimer.repeatCount;
		}
		private function onStopTimer(event:TimerEvent):void{
		}
		private function onFadeComplete(event:TimerEvent):void{
			this.alpha = 1.0;
			this.fadeInTimer.reset();
			this.stopTimer.start();
		}
		private function onStopComplete(event:TimerEvent):void{
			this.visible = false;
			this.stopTimer.reset();
		}
		public function show():void{
			this.visible = true;
			this.alpha = 0.0;
			this.fadeInTimer.start();
		}
		
	}
}